﻿using UnityEngine;

//和存档系统交互,控制游戏进程
public class GameManager : Service
{
    public SaveManager saveManager;
    private SceneController sceneController;
    public LayerMask groundLayer;
    public LayerMask playerLayer;

    private int _StartCheckPointIndex;
    public int StartCheckPointIndex 
    { 
        //继续游戏时，从记录的存档点开始，之后的关卡依然从0存档点开始
        get
        {
            int temp = _StartCheckPointIndex;
            _StartCheckPointIndex = 0;
            return temp;
        }
        private set
        {
            if (value < 0 || value > 2)
                value = 0;
            _StartCheckPointIndex = value;
        }
    }

    private void Start()
    {
        DontDestroyOnLoad(gameObject);
        sceneController = Service.Get<SceneController>();
        saveManager.Initialize();
    }

    public void StartGame(int sceneIndex)
    {
        if (sceneIndex <= 0) 
            sceneIndex = 1;
        sceneController.LoadLevel(sceneIndex);
        StartCheckPointIndex = 0;
    }

    public void ContinueGame()
    {
        saveManager.Load();
        SaveData s = saveManager.SaveData;

        if (s.SceneIndex <= 0) 
            s.SceneIndex = 1;      
        sceneController.LoadLevel(s.SceneIndex);
        StartCheckPointIndex = s.CheckPointIndex;
    }
}
